﻿<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="DTOTemplatedGenerator.MainWindow"
        Title="Generate code from DTO" WindowState="Maximized">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Margin="4,2">
            <Button Content="Save data" Background="Orange" ToolTip="Save Templates and Types transf. to file near .exe" Click="onSaveData"/>
            <Button Content="Import" Margin="8,0,4,0" ToolTip="Import Templates and Types transf. from Clipboard" Click="onImportData"/>
            <Button Content="Export" Margin="4,0" ToolTip="Export Templates and Types transf. to Clipboard" Click="onExportData"/>
        </StackPanel>
        <TabControl Grid.Row="1">
            <TabItem x:Name="tabGenerator" Header="Generator" IsSelected="True">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal" FocusManager.IsFocusScope="True" Grid.ColumnSpan="2">
                        <Button Margin="0,0,4,0" Click="onSelectDeselect" Content="*" />
                        <Button ToolTip="Parse code from Clipboard" Content="Parse code" Command="{Binding ParseClipBoardCode}" />
                        <Button ToolTip="Parse tab separated values" Content="Parse Tabed data" Command="{Binding ParseTabedValuesCommand}" Margin="4,0" />
                        <Button ToolTip="Parse Coma Separeted Values from Clipboard" Content="Parse CSV" Command="{Binding ParseCSVCommand}" />
                        <TextBlock VerticalAlignment="Center" Margin="4,0"><Run Text="Templates"/></TextBlock>
                        <Button x:Name="btnSetTempl" Margin="4,0" ToolTip="Set selected Template to selected props" Content="SetTemplate" 
							Click="onShowTemplMenu" >
                            <Button.ContextMenu>
                                <ContextMenu x:Name="menuTemplates"/>
                            </Button.ContextMenu>
                        </Button>
                        <Button Background="LightGreen" Content="Generate" Command="{Binding GenerateCommand}"/>
                        <Button Margin="4,0" ToolTip="Edit values off all selected rows in same time" Content="Edit values" Click="onEditValues"/>
                        <Button Margin="4,0" ToolTip="Remove selected props" Content="Remove props" Click="onRemoveProps"/>
                    </StackPanel>
                    <DataGrid Grid.Row="1" x:Name="dataGridProps" ItemsSource="{Binding Props}"
						AllowDrop="True"
						AutoGenerateColumns="False" LostKeyboardFocus="onLostKeybordFocusDataGrid">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="@PropName" Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Width="100"/>
                            <DataGridTextColumn Header="@OrgPropType" Binding="{Binding Type, UpdateSourceTrigger=PropertyChanged}" Width="100"/>
                            <DataGridTextColumn Header="@Attr" Binding="{Binding Attr, UpdateSourceTrigger=PropertyChanged}" Width="100"/>
                            <DataGridTemplateColumn Header="Template">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Template.Name}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate DataType="DataGridCell">
                                        <ComboBox ItemsSource="{Binding DataContext.PropTemplates, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}"
											SelectedItem="{Binding Template, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTextColumn Header="@PropType" Binding="{Binding TransType}"/>
                        </DataGrid.Columns>
                    </DataGrid>
                    <TextBox AcceptsReturn="True" Grid.Row="1" Grid.Column="1" Text="{Binding GeneratedResult}"/>
                </Grid>
            </TabItem>
            <TabItem Header="Prop. Templates">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal">
                        <!--<Button Padding="4,2" Background="LightGreen" ToolTip="Save template to file which is in the same folder as .exe" Content="Save"  
                                Click="onSaveTemplates" 
                                HorizontalAlignment="Left"/>-->
                        <TextBlock VerticalAlignment="Center" Margin="4,0"><Run Text="Types trans"/></TextBlock>
                        <ComboBox x:Name="comboTypesTrans" MinWidth="100" ItemsSource="{Binding TypesTrans}" DisplayMemberPath="Name"/>
                        <Button Margin="4,0" ToolTip="Set selected Types Transformation to selected templates" Content="Set Type trans" 
							Click="onSetTypesTrans" />
                        <Button ToolTip="Remove selected templates" Content="Remove templates" Click="onRemoveTemplates"/>
                    </StackPanel>
                    <DataGrid Grid.Row="1" x:Name="dataGridTemplates" ItemsSource="{Binding PropTemplates}" 
						AutoGenerateColumns="False" LostKeyboardFocus="onLostKeybordFocusDataGrid">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="200"/>
                            <DataGridTemplateColumn Header="Text" Width="500">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Text}" MaxHeight="40" ToolTip="{Binding Text}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate DataType="DataGridCell">
                                        <TextBox AcceptsReturn="True" MaxHeight="40" Text="{Binding Text}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button Grid.Column="1" Content="..." Width="20" PreviewMouseLeftButtonDown="onEditTemplate" VerticalAlignment="Center"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn Header="Types transform">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding TypesMatch}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate DataType="DataGridCell">
                                        <ComboBox ItemsSource="{Binding DataContext.TypesTrans, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}"
											VerticalAlignment="Top" SelectedValue="{Binding TypesMatch}" SelectedValuePath="Name" DisplayMemberPath="Name"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTextColumn Header="Category"                                                 
								Binding="{Binding Category, UpdateSourceTrigger=PropertyChanged}" 
								Width="100"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </Grid>
            </TabItem>
            <TabItem Header="Types transform">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal">
                        <!--<Button Padding="4,2" Margin="4,0" Background="LightGreen" ToolTip="Save types matches to file which is in the same folder as .exe" Content="Save"  
                                Click="onSaveTypesMatch" 
                                HorizontalAlignment="Left"/>-->
                        <Button Content="Remove types transformations" ToolTip="Remove selected types transformations" Click="onRemoveTypeTrans"/>
                        <TextBlock Margin="4,0" VerticalAlignment="Center"><Run Text="Source can contain '*' as any, expample: 'varchar*'"/></TextBlock>
                    </StackPanel>
                    <DataGrid Grid.Row="1" x:Name="dataGridTypes" ItemsSource="{Binding TypesTrans}" 
						AutoGenerateColumns="False" LostKeyboardFocus="onLostKeybordFocusDataGrid">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="200"/>
                            <DataGridTemplateColumn Header="Matches" Width="500">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding MatchesView}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate DataType="DataGridCell">
                                        <DataGrid ItemsSource="{Binding Matchs}" MinHeight="40"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
                </Grid>
            </TabItem>
            <TabItem Header="General Templates">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <StackPanel Orientation="Horizontal">
                        <Button Background="LightGreen" Content="Generate" Click="onGenGeneralTemplate"/>
                        <Button Margin="4,0" Content="Remove template" ToolTip="Remove selected templates" Click="onRemoveGeneralTemplates"/>
                    </StackPanel>
                    <DataGrid Grid.Row="1" Grid.RowSpan="2" x:Name="dataGridGeneralTemplates" ItemsSource="{Binding GeneralTemplates}" 
						AutoGenerateColumns="False" LostKeyboardFocus="onLostKeybordFocusDataGrid">
                        <DataGrid.Columns>
                            <!--<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="200"/>-->
                            <DataGridTemplateColumn Header="Data" Width="300">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding DataView}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate DataType="DataGridCell">
                                        <DataGrid ItemsSource="{Binding Data}" MinHeight="40" AutoGenerateColumns="False">
                                            <DataGrid.Columns>
                                                <DataGridTextColumn Header="@Key" Width="100" Binding="{Binding Key}"/>
                                                <DataGridTextColumn Header="Value" Width="100" Binding="{Binding Value}"/>
                                            </DataGrid.Columns>
                                        </DataGrid>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn Header="Template" Width="400">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock MaxHeight="40" Text="{Binding Template}" ToolTip="{Binding Template}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate DataType="DataGridCell">
                                        <TextBox AcceptsReturn="True" MaxHeight="40" Text="{Binding Template}" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button Grid.Column="1" Content="..." Width="20" PreviewMouseLeftButtonDown="onEditGenTemplate" VerticalAlignment="Center"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
                    <StackPanel Grid.Column="1" Grid.Row="1" Margin="4,0">
                        <TextBlock VerticalAlignment="Center"><Run Text="Use @Key to insert values in right place. Example: Key = key1, in template: @key1"/></TextBlock>
                        <TextBlock VerticalAlignment="Center"><Run Text="To generate all props and insert result: @GenerateProps(Your template name)"/></TextBlock>
                    </StackPanel>
                    <TextBox Text="{Binding GeneralTemGenResult}" Grid.Column="1" AcceptsReturn="True" Grid.Row="2"/>
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</Window>
